package com.goods.service.impl;

import com.goods.business.mapper.HealthMapper;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.service.HealthService;
import com.goods.system.mapper.UserMapper;
import com.goods.system.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class HealthServiceImpl implements HealthService {

    @Autowired
    HealthMapper healthMapper;

    @Autowired
    UserService userService;

    @Autowired
    UserMapper userMapper;

    @Override
    public Health isReport() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        Health health = healthMapper.selectToday(format);
        try {
            UserInfoVO info = userService.info();
            User user = new User();
            BeanUtils.copyProperties(info,user);
            User user1 = userMapper.selectOne(user);
            health.setUserId(user1.getId());
            return healthMapper.selectOne(health);
        } catch (SystemException e) {
            e.printStackTrace();
        }
       return null;
    }

    @Override
    public int report(HealthVO healthVO) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);

        try {
            UserInfoVO userInfoVO = userService.info();
            User user = new User();
            user.setUsername(userInfoVO.getUsername());
            User user1 = userMapper.selectOne(user);
            health.setUserId(user1.getId());
        } catch (SystemException e) {
            e.printStackTrace();
        }
        health.setCreateTime(new Date());
        return healthMapper.insert(health);
    }

    @Override
    public List<Health> history() {
        return healthMapper.selectAll();
    }
}
